package com.xuecheng.learning.controller;

import com.xuecheng.api.learning.CourseRecordApi;
import com.xuecheng.api.learning.model.dto.CourseRecordDTO;
import com.xuecheng.api.learning.model.vo.ResourceUrlModel;
import com.xuecheng.common.domain.code.CommonErrorCode;
import com.xuecheng.common.domain.page.PageRequestParams;
import com.xuecheng.common.domain.page.PageVO;
import com.xuecheng.common.domain.response.RestResponse;
import com.xuecheng.common.domain.uaa.LoginUser;
import com.xuecheng.learning.common.utils.UAASecurityUtil;
import com.xuecheng.learning.service.CourseRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 选课记录 前端控制器
 * </p>
 *
 * @author itcast
 */
@Slf4j
@RestController
public class CourseRecordController implements CourseRecordApi {

    @Autowired
    private CourseRecordService  courseRecordService;


    @Override
    @PostMapping("learnedRecords/list")
    public PageVO<CourseRecordDTO> queryCourseRecordList(PageRequestParams params) {

        return courseRecordService.queryCourseRecordList(params);
    }

        @GetMapping("learnedRecords/myCourseRec/{coursePubId}")
    public CourseRecordDTO getRecordByCoursePubId(@PathVariable Long coursePubId) {
        String username = UAASecurityUtil.getUser().getUsername();
        CourseRecordDTO dto = courseRecordService.getRecordByCoursePubId(coursePubId, username);
        return dto;
    }

    @PostMapping("l/course-record/paid/{username}/{coursePubId}")
    public RestResponse<CourseRecordDTO> createCourseRecord4S(@PathVariable String username,
                                                              @PathVariable Long coursePubId) {
        return courseRecordService.createCourseRecord4S(username, coursePubId);
    }


    @GetMapping("resource-url")
    public ResourceUrlModel getResourceUrl(Long coursePubId, Long mediaId, Long teachplanId) {

        LoginUser user = UAASecurityUtil.getUser();

        if (ObjectUtils.isEmpty(user)) {
            ResourceUrlModel urlModel = new ResourceUrlModel();
            urlModel.wrong(CommonErrorCode.E_100108);
            return urlModel;
        }

        String username = user.getUsername();

        ResourceUrlModel resourceUrl = courseRecordService.getResourceUrl(coursePubId, mediaId, teachplanId, username);

        return resourceUrl;
    }


    @GetMapping("personCount")
    public Integer getCount(@RequestParam Long coursePubId){
        return courseRecordService.getCount(coursePubId);
    }

}
